package com.example.demo.Controller;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.GeneralResponse.APIResponse;
import com.example.demo.Service.HRService;
import com.example.demo.Service.TokenService;
import com.example.demo.utils.DateFormatter;


/**
 * @version: V0.3
 * @author: 郑皓杰 吴嘉鑫
 * @className: ManageController
 * @packageName: com.example.demo.Controller
 * @description: 这是HR进行本公司员工管理的控制类
 * @data: 2021-07-14
 **/

@RequestMapping("/Manage")
@RestController
public class ManageController {
	
	@Autowired
	private HRService hrService;
	@Autowired
	private TokenService tokenService;
	
	/**
	* @author:吴嘉鑫
	* @methodsName: queryAll
	* @description: 查询所在公司在职全部员工
	* @param:  
	* @return: APIResponse
	* @throws: 
	*/
	@CrossOrigin
	@RequestMapping("/queryAllOn")
	public APIResponse<?> queryAllOn(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse){
		String token = httpServletRequest.getHeader("Token");
		Integer companyID = tokenService.getCompanyIDfromToken(token); 
		Integer page=Integer.parseInt(httpServletRequest.getParameter("page"));
		return hrService.queryOnEmpBycompanyID(companyID, page);
	}
	
	/**
	* @author:吴嘉鑫
	* @methodsName: queryAllOff
	* @description: 查询所在公司离职全部员工
	* @param:  
	* @return: APIResponse
	* @throws: 
	*/
	@CrossOrigin
	@RequestMapping("/queryAllOff")
	public APIResponse<?> queryAllOff(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse){
		String token = httpServletRequest.getHeader("Token");
		Integer companyID = tokenService.getCompanyIDfromToken(token); 
		Integer page=Integer.parseInt(httpServletRequest.getParameter("page"));
		return hrService.queryOffEmpBycompanyID(companyID, page);
	}
	
	/**
	* @author:吴嘉鑫
	* @methodsName: queryeXP
	* @description: 查询某个员工在本公司的信息
	* @param:  
	* @return: APIResponse
	* @throws: 
	*/
	@CrossOrigin
	@RequestMapping("/manageexp")
	//查询某个员工在本公司的信息
	public APIResponse<?> queryeXP(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse) {
		//Integer companyID=((HumanResource)httpServletRequest.getSession().getAttribute("hr")).getCompanyID();
		String token = httpServletRequest.getHeader("Token");
		Integer companyID = tokenService.getCompanyIDfromToken(token); 
		String tel = httpServletRequest.getParameter("tel");
		return hrService.queryexp(tel,companyID);
	}
	
	/**
	* @author:吴嘉鑫
	* @methodsName: queryOne
	* @description: 用nameortel查询所在公司员工
	* @param:  
	* @return: APIResponse
	* @throws: 
	*/
	@CrossOrigin
	@RequestMapping("/queryOne")
	public APIResponse<?> queryOne(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse){
		String token = httpServletRequest.getHeader("Token");
		String nameortel = httpServletRequest.getParameter("nameortel");
		Integer companyID = tokenService.getCompanyIDfromToken(token); 
		return hrService.queryOurEmpBynameortel(companyID,nameortel);
	}
	
	/**
	* @author:吴嘉鑫
	* @methodsName: queryOnPage
	* @description: 获取本公司所有员工所占页数
	* @param:  HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse
	* @return: APIResponse
	* @throws: 
	*/
	@CrossOrigin
	@RequestMapping("/queryOnPage")
	public APIResponse<?> queryOnPage(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse){
		String token = httpServletRequest.getHeader("Token");
		Integer companyID = tokenService.getCompanyIDfromToken(token); 
		return hrService.pageOnsum(companyID);
	}
	
	/**
	* @author:吴嘉鑫
	* @methodsName: queryOffPage
	* @description: 获取本公司所有员工所占页数
	* @param:  HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse
	* @return: APIResponse
	* @throws: 
	*/
	@CrossOrigin
	@RequestMapping("/queryOffPage")
	public APIResponse<?> queryOffPage(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse){
		String token = httpServletRequest.getHeader("Token");
		Integer companyID = tokenService.getCompanyIDfromToken(token); 
		return hrService.pageOffsum(companyID);
	}
	
	/**
	* @author:郑皓杰
	* @methodsName: writePerformance
	* @description: 对本公司某员工写绩效
	* @param:  HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse
	* @return: APIResponse
	* @throws: 
	*/
	@CrossOrigin
	@RequestMapping("/writePerformance")
	public APIResponse<?> writePerformance(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse){
		 Integer expid=Integer.parseInt(httpServletRequest.getParameter("expid"));
		 Integer rank=Integer.parseInt(httpServletRequest.getParameter("rank"));
		 Date start=DateFormatter.DateTransform(httpServletRequest.getParameter("start"));
		 Date end=DateFormatter.DateTransform(httpServletRequest.getParameter("end"));
		 String title=httpServletRequest.getParameter("title");
		 String text=httpServletRequest.getParameter("text");
		return hrService.writePerformance(expid,rank,title,text,start,end);
	}
	/**
	* @author:郑皓杰
	* @methodsName: modifyPerformance
	* @description: 修改本公司某员工绩效
	* @param:  HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse
	* @return: APIResponse
	* @throws: 
	*/
	@CrossOrigin
	@RequestMapping("/modifyPerformance")
	public APIResponse<?> modifyPerformance(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse){
		 Integer perid=Integer.parseInt(httpServletRequest.getParameter("perid"));
		 String title=httpServletRequest.getParameter("title");
		 String text=httpServletRequest.getParameter("text");
		return hrService.modifyPerformance(perid,title,text);
	}
	
	/**
	* @author:郑皓杰
	* @methodsName: deletePerformance
	* @description: 删除本公司某员工绩效
	* @param:  HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse
	* @return: APIResponse
	* @throws: 
	*/
	@CrossOrigin
	@RequestMapping("/deletePerformance")
	public APIResponse<?> deletePerformance(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse){
		 Integer perid=Integer.parseInt(httpServletRequest.getParameter("perid"));
		return hrService.deletePerformance(perid);
	}
	
	/**
	* @author:吴嘉鑫
	* @methodsName: sendMail
	* @description: 给心仪的员工发邮件
	* @param:  HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse
	* @return: APIResponse
	* @throws: 
	*/
	@CrossOrigin
	@RequestMapping("/sendMail")
	public APIResponse<?> sendMail(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse)throws Throwable{
		String email = httpServletRequest.getParameter("email");
		String text = httpServletRequest.getParameter("text");
		String subject = httpServletRequest.getParameter("subject");

		return hrService.sendEmail(email, subject, text);
	}
	
	
	
	
	
}
